AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( "shared.lua" )

Model( "models/props_combine/health_charger001.mdl" )

function ENT:Initialize()
	-- Set model.
	self.Entity:SetModel( "models/props_combine/suit_charger001.mdl" )
	
	-- Initialise physics.
	self.Entity:PhysicsInitBox( self:OBBMins(), self:OBBMaxs() )
	self.Entity:SetCollisionBounds( self:OBBMins(), self:OBBMaxs() )
	
	-- Freeze!
	local phys = self:GetPhysicsObject()
	if ( phys:IsValid() ) then
		phys:EnableMotion( false )
	end
	
	// Custom physics makes entity a dull boy. I mean, wrongly orientated.
	if ( self:GetKeyValue( "angles", -1 ) != -1 ) then
		local angle = {}
		for k, v in pairs( string.Explode( " ", self:GetKeyValue( "angles" ) ) ) do
			angle[ k ] = tonumber( v ) or 0
		end
		self:SetAngles( Angle( unpack( angle ) ) )
	end
	
	self:SetUseType( SIMPLE_USE )
	
	-- Set default values.
	self.MaxHealth = self:GetKeyValue( "health", 750 )
	self.MaxLength = 256
	self.HealDelay = 0.1
	
	self:SetPropHealth( self.MaxHealth )
	self:SetPropMaxHealth( self.MaxHealth )
	
	self.Players = {}
end

function ENT:Use( ply )
	if ( !ply || !IsValid( ply ) || !ply:IsPlayer() ) then return end
	local health = ply:Health()
	if ( health < 100 ) then
		local add = self:TakeHP( 100 - health )
		ply:SetHealth( health + add )
	end		
end
function ENT:TakeHP( amount )
	amount = math.min( amount, self:GetPropHealth() )
	self:SetPropHealth( self:GetPropHealth() - amount, true )
	self:SetAnimationPosition()
	return amount
end
function ENT:SetAnimationPosition()
	self.Entity:SetPlaybackRate( 0.0 )
	self.Entity:ResetSequence( 0 )
	self.Entity:SetCycle( 1 - ( self:GetPropHealth() / self:GetPropMaxHealth() ) )
end

function ENT:OnTakeDamage( dmg )
	gamemode.Call( "EntityTakeDamage", self, dmg:GetInflictor(), dmg:GetAttacker(), dmg:GetDamage(), dmg )
end

function ENT:GetKeyValue( key, default )
	if ( !self.KeyValues || !self.KeyValues[ key ] ) then return default end
	return self.KeyValues[ key ]
end
function ENT:KeyValue( key, value )
	self.KeyValues = self.KeyValues or {}
	self.KeyValues[ key ] = value
end
